package service

import (
	"crow-qin/app/config"
	"crow-qin/app/models"
	"crow-qin/app/pkg"
	"errors"
	"github.com/toolkits/pkg/logger"
)

type SysUser struct {
	models.SysUser
}

func CreateUser(userName, password, email, phone, role string) (user *SysUser) {
	user = new(SysUser)
	user.UserName = userName
	user.Password = pkg.EncryptWithMd5(password)
	user.Email = email
	user.Phone = phone
	user.Role = role
	return user
}

// Create
// 创建用户
func (u *SysUser) Create() (userId int64, err error) {
	tx := config.MyTx.Begin()
	defer func() {
		if err != nil {
			tx.Rollback()
		} else {
			tx.Commit()
		}
	}()
	//查重
	checkResult, err := models.CheckUserExists(u.UserName, tx)
	if checkResult == true {
		err = errors.New("用户名已经存在")
		logger.Error(err)
		return 0, err
	} else {
		logger.Info("用户名查重通过")
	}

	//创建用户
	userId, err = u.SysUser.Create(tx)
	if err != nil {
		logger.Error(err)
		return userId, nil
	}
	return userId, nil
}

// DeleteUser
// 删除用户
func (u *SysUser) DeleteUser() (err error) {
	tx := config.MyTx.Begin()
	defer func() {
		if err != nil {
			tx.Rollback()
		} else {
			tx.Commit()
		}
	}()
	//检查用户是否存在
	mSysUser, err := u.SysUser.SelectUserById(tx)
	if err != nil {
		logger.Error(err)
		return err
	}
	if mSysUser.ID == 0 {
		err = errors.New("用户不存在")
		return err
	}

	//删除用户
	err = u.SysUser.DeleteUser(tx)
	if err != nil {
		logger.Error(err)
		return err
	}
	return nil
}

// Update
// 根据id更新用户信息
func (u *SysUser) Update() (err error) {
	tx := config.MyTx.Begin()
	defer func() {
		if err != nil {
			tx.Rollback()
		} else {
			tx.Commit()
		}
	}()

	//检查用户输入
	if u.ID == 0 {
		err = errors.New("请传入用户id")
		return err
	} else {
		//检查用户是否存在
		mSysUser, err := u.SysUser.SelectUserById(tx)
		if err != nil {
			logger.Error(err)
			return err
		}
		if mSysUser.ID == 0 {
			err = errors.New("用户不存在")
			return err
		}
	}

	//根据id更新用户信息
	err = u.UpdateUserByID(tx)
	if err != nil {
		logger.Error(err)
		return err
	}
	return nil
}

// GetUserList
// 分页查询
func (u *SysUser) GetUserList(pageSize int64, pageNum int64) (sysUserList []SysUser, count int64, err error) {
	tx := config.MyTx.Begin()
	defer func() {
		if err != nil {
			tx.Rollback()
		} else {
			tx.Commit()
		}
	}()

	sysUsers, count, err := u.SelectUsersByUserName(pageSize, pageNum, tx)
	if err != nil {
		logger.Error(err)
		return nil, 0, err
	}
	for _, mU := range sysUsers {
		sU := SysUser{
			mU,
		}
		sysUserList = append(sysUserList, sU)
	}

	return sysUserList, count, nil
}

// Get
// 根据用户ID获取用户信息
func (u *SysUser) Get() (err error) {
	tx := config.MyTx.Begin()
	defer func() {
		if err != nil {
			tx.Rollback()
		} else {
			tx.Commit()
		}
	}()
	u.SysUser, err = u.SelectUserById(tx)
	if err != nil {
		logger.Error(err)
		return err
	}
	return nil
}

// GetUserByUserName
// 根据用户名获取用户信息
func (u *SysUser) GetUserByUserName() (err error) {
	tx := config.MyTx.Begin()
	defer func() {
		if err != nil {
			tx.Rollback()
		} else {
			tx.Commit()
		}
	}()

	user, err := u.SelectUserByUserName(tx)
	if err != nil {
		logger.Error(err)
		return err
	}
	if user.ID > 0 {
		u.SysUser = user
	} else {
		err = errors.New("未查询到用户信息")
	}
	return nil
}
